<?php
##############################################
#
#   分类管理
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\GoodsModel;
use Admin\Model\AdminBaseModel;
use Admin\Model\ShopModel;
use Admin\Model\CategoryModel;
use Admin\Model\LocalityModel;

class GoodsController extends AdminBaseController {
	
	protected $_goods;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _goods  = new GoodsModel();
	}
	
	/**
	 * 商品列表
	 */
	public function goodsList(){
		if(!IS_GET) return;
		$goods_data = $this -> _goods -> getGoods();
		$this -> assign('goods', $goods_data);
		$this -> display('goods_list');
	}
	
	/**
	 * 添加商品
	 */
	public function addGoods(){
		if(IS_GET){
			$shop_model = new ShopModel();
			$category_model = new CategoryModel();
			$locality_model = new LocalityModel();
			
			$shop_data = $shop_model -> getShop();
			$category = $category_model -> getCategory();
			$category_data = $category_model -> disData($category);
			$locality = $locality_model -> getLocality();
			$locality_data = $locality_model -> disData($locality);
			
			$this -> assign('shop', $shop_data);
			$this -> assign('category', $category_data);
			$this -> assign('locality', $locality_data);
			$this -> assign('goods_server',C('goods_server')); 
			$this -> display('goods_add');
		}else if(IS_POST){
			$data = array(
		    	'shopid' => I('post.shopid'),
				'cid' => I('post.cid'),
				'lid' => I('post.lid'),
				'main_title' => I('post.main_title'),
				'sub_title' => I('post.sub_title'),
				'price' => I('post.price'),
				'old_price' => I('post.old_price'),
				'begin_time' => strtotime(I('post.begin_time')),
				'end_time' => strtotime(I('post.end_time'))
			);
			$detail = array(
				'goods_server' => serialize(I('post.goods_server[]')),
				'detail' => I('post.detail')
			);
			/* var_dump($data);
			var_dump($detail);
			exit; */
			$result = $this -> _goods -> addGoods($data, $detail);
			if($result)
				$this -> success('添加成功', 'goodsList', 1);
			else
				$this -> error('操作失败', '', 1);
		}
		
	}
	
	/**
	 * 删除商品
	 */
	public function deleteShop(){
		if(!IS_AJAX) return;
		$result = $this -> _goods -> deleteShop(intval($_GET['shopid']));
		if($result)
			$this -> ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
			$this -> ajaxReturn(array('status' => 2,'message' => '操作失败'));
	}
	
}



	
	